package com.hmo.appshop.mamager.dto.commom;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import org.springframework.http.HttpStatus;

import java.util.Date;

@ApiModel("json外层")
@NoArgsConstructor
@AllArgsConstructor
@Data
@Accessors(chain = true)
public class Result<T> {

    @ApiModelProperty("响应码")
    private Integer code;

    @ApiModelProperty("描述")
    private String message;

    @ApiModelProperty("数据")
    private T data;

    @ApiModelProperty("时间戳")
    private Long timestamp;

    public Result<T> ok() {
        return new Result<>(HttpStatus.OK.value(), "succeed", null, new Date().getTime());
    }

    public Result<T> ok(String message) {
        return new Result<>(HttpStatus.OK.value(), message, null, new Date().getTime());
    }

    public Result<T> ok(T data) {
        return new Result<>(HttpStatus.OK.value(), "succeed", data, new Date().getTime());
    }

    public Result<T> ok(String message, T data) {
        return new Result<>(HttpStatus.OK.value(), message, data, new Date().getTime());
    }

    public Result<T> serverError() {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器异常", null, new Date().getTime());
    }

    public Result<T> serverError(String message) {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, null, new Date().getTime());
    }

    public Result<T> serverError(T data) {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), "服务器异常", data, new Date().getTime());
    }

    public Result<T> serverError(String message, T data) {
        return new Result<>(HttpStatus.INTERNAL_SERVER_ERROR.value(), message, data, new Date().getTime());
    }

    //未授权
    public Result<T> Gone(String message) {
        return new Result<>(HttpStatus.GONE.value(), message, null, new Date().getTime());
    }

    //错误了
    public Result<T> fail(String message) {
        return new Result<>(HttpStatus.GONE.value(), message, null, new Date().getTime());
    }

    public Result<T> message(String message) {
        this.message = message;
        return this;
    }

    public Result<T> code(Integer code) {
        this.code = code;
        return this;
    }


    public Result<T> data(T data) {
        this.data = data;
        return this;
    }


    public Result<T> addCodeMessage(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
        return this;
    }

    public Result<T> addCodeMessage(Integer code, String message) {
        this.code = code;
        this.message = message;
        return this;
    }
}
